Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • _{ invalid name r(198) / error in foreach (loop)

    Hello everyone! I'm having trouble with this "foreach" loop, after setting a local (called bm) :


    local bm IADM92 BSAS BSAGI IADM93 BSMJSC BSMJGI IADM120 IADM95 IADM94

    set trace on

    foreach var of local `bm' {
    generate log_`var' = ln(`var'), after(`var')
    drop `var'
    }

    result:
    - foreach var of local `bm'{
    = foreach var of local {
    _{ invalid name
    r(198);

    end of do-file

    I do not know what is trying to say with that error.

    Thanks!

  • #2
    I believe your problem is that you have written your code in the do-file editor window, and then rather than running everything at once, you are running it by selecting a few lines and running them, then selecting the next few lines and running them, and so on.

    Consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
    Code:
    . do "/Users/lisowskiw/Downloads/example.do"
    
    . local message Hello, world.
    
    . display "The message is `message'"
    The message is Hello, world.
    
    . 
    end of do-file
    
    .
    Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
    Code:
    . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD04017.000000"
    
    . local message Hello, world.
    
    . 
    end of do-file
    
    . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD04017.000000"
    
    . display "The message is `message'"
    The message is 
    
    . 
    end of do-file
    
    .
    The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

    Comment


    • #3
      William Lisowski is correct. But there is an additional problem that will bite once that is fixed.

      Code:
      foreach var of local `bm' {
      
      SHOULD BE
      
      foreach var of local bm { // N.B.  NO QUOTES!!!!
      When using -foreach var of local ... {-, the ... must be the local macro's name. The incorrect `bm' (with quotes) would be replaced by the contents of local macro bm, and consequently Stata would interpret the original code as -foreach var of local IADM92 BSAS BSAGI IADM93 BSMJSC BSMJGI IADM120 IADM95 IADM94-. But that would be a syntax error because only one local macro name is allowed. And if local bm contained only IADM92, it would still be a problem because no local macro IADM92 has ever been defined, at least not within the code shown. So local macro IADM902 would be interpreted as containing an empty string. So you would be looping over "nothing" and the entire loop would just be skipped!

      Comment


      • #4
        Awesome! Thank you all!

        Comment

        Working...
        X